package com.repair.controller;

import com.repair.entity.RepairTrack;
import com.repair.service.RepairTrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/repair-track")
public class RepairTrackController {

    @Autowired
    private RepairTrackService repairTrackService;

    @PostMapping
    public ResponseEntity<?> addTrack(@RequestBody Map<String, Object> track) {
        Long orderId = Long.parseLong(track.get("orderId").toString());
        Long repairmanId = Long.parseLong(track.get("repairmanId").toString());
        Double latitude = Double.parseDouble(track.get("latitude").toString());
        Double longitude = Double.parseDouble(track.get("longitude").toString());
        Integer status = (Integer) track.get("status");

        repairTrackService.addTrack(orderId, repairmanId, latitude, longitude, status);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/order/{orderId}")
    public ResponseEntity<?> getOrderTracks(@PathVariable Long orderId) {
        List<RepairTrack> tracks = repairTrackService.getOrderTracks(orderId);
        return ResponseEntity.ok(tracks);
    }

    @GetMapping("/repairman/{repairmanId}")
    public ResponseEntity<?> getRepairmanTracks(@PathVariable Long repairmanId) {
        List<RepairTrack> tracks = repairTrackService.getRepairmanTracks(repairmanId);
        return ResponseEntity.ok(tracks);
    }

    @PutMapping("/{trackId}/status")
    public ResponseEntity<?> updateTrackStatus(
            @PathVariable Long trackId,
            @RequestBody Map<String, Integer> request) {
        repairTrackService.updateTrackStatus(trackId, request.get("status"));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/order/{orderId}/status/{status}")
    public ResponseEntity<?> getOrderTracksByStatus(
            @PathVariable Long orderId,
            @PathVariable Integer status) {
        List<RepairTrack> tracks = repairTrackService.getOrderTracksByStatus(orderId, Integer.valueOf(String.valueOf(status)));
        return ResponseEntity.ok(tracks);
    }
} 